import EventCenter from "../../lib/EventCenter";
import GameCfgData from "../../lib/GameCfgData";
import MyDialogManager from "../../lib/MyDialogManager";
import Sdk from "../../lib/Sdk";
import SoundManager from "../../lib/SoundManger";
import { TipManager } from "../../lib/TipManager";
import {ui} from "../../ui/layaMaxUI"
import EventEunm from "../data/EventEunm";
import PlayerData from "../data/PlayerData";
import RankManager from "../RankManager";
export default class StartScsene extends ui.StartSceneUI{
    constructor(){
        super();
    }

    onEnable(){
        this.height=this.bg.height=Laya.stage.height;
        this.eidtBtn.on(Laya.Event.CLICK,this,this.eidtClick);
        this.startBtn.on(Laya.Event.CLICK,this,this.startClick)
        this.skinBtn.on(Laya.Event.CLICK,this,this.skinClick);
        this.wujinBtn.on(Laya.Event.CLICK,this,this.wujinClick);
        this.rankBtn.on(Laya.Event.CLICK,this,this.rankClick);
        this.contactBtn.on(Laya.Event.CLICK,this,this.contactClick);
        this.startBtn.label="第"+PlayerData.getIns().currGate+"关";
        if(Sdk.getIns().isLongScene())Sdk.getIns().showBanner();
        this.eidtBtn.visible=Sdk.getIns().isWeb;
        this.shareBtn.visible=PlayerData.getIns().getDayTaskSt(2)==0;
        if(this.shareBtn.visible)this.shareBtn.once(Laya.Event.CLICK,this,this.shareClick);
        SoundManager.playMiuse("bg5.mp3")
        if(!PlayerData.getIns().id){
            PlayerData.getIns().createId();
            // if(Sdk.getIns().sdkEnvironment){
            //     Sdk.getIns().sdkEnvironment.login({
            //         force: false,
            //         success(res) {
            //             RankManager.getIns().getOpenId("wxbd5459b7ecc54ec2",res.code,res.anonymous_code,(res2)=>{
            //                 let topenid = res2.openId;
            //                 if(topenid){
            //                     PlayerData.getIns().openId=topenid;
            //                 }
            //             })
            //         }
            //     })
            // }else{
                RankManager.getIns().addDataToDB();
            // }
            
        }
        let tnoticedata = GameCfgData.getIns().getNotice();
        if(tnoticedata && tnoticedata.id != PlayerData.getIns().noticeId){
            MyDialogManager.getIns().sceneOpen("Notice.scene",true,tnoticedata);
        }
    }

    private eidtClick(){
        MyDialogManager.getIns().sceneOpen("EidtScene.scene",true);
    }

    private startClick(){
        if(GameCfgData.getIns().isLoading){
            TipManager.ins.showMsgAlert("新关卡正在加载中,稍等再试试",this);
            return;
        }
        if(!GameCfgData.getIns().getGateCfg(PlayerData.getIns().currGate)){
            TipManager.ins.showMsgAlert("新关卡正在筹备中,你可以尝试无尽模式哦",this);
            return ;
        }
        if(PlayerData.getIns().live<=0){
            MyDialogManager.getIns().sceneOpen("BuyLiveDialog.scene");
            return;
        }
        MyDialogManager.getIns().sceneOpen("GameScene.scene",true);
    }

    private wujinClick(){
        if(PlayerData.getIns().currGate<5){
            TipManager.ins.showMsgAlert("第五关解锁",this);
            return;
        }
       
        MyDialogManager.getIns().sceneOpen("GameScene.scene",true,true);
    }

    private rankClick(){
        if(PlayerData.getIns().currGate<5){
            TipManager.ins.showMsgAlert("尚未解锁无尽模式",this);
            return;
        }
        MyDialogManager.getIns().sceneOpen("RankDialog.scene");
    }

    private skinClick(){
        MyDialogManager.getIns().sceneOpen("SkinScene.scene",true);
    }

    private contactClick(){
        MyDialogManager.getIns().sceneOpen("ContactUsDialog.scene",false);
    }

    private shareClick(){
        Sdk.getIns().share();
        Laya.timer.once(1000,this,()=>{
            this.shareBtn.visible=false;
            PlayerData.getIns().addGold(100);
            EventCenter.getIns().event(EventEunm.goldChange);
            PlayerData.getIns().setDayTaskAwardSt(2,2);
        })
    }

    onDisable(){
        this.eidtBtn.off(Laya.Event.CLICK,this,this.eidtClick);
        this.startBtn.off(Laya.Event.CLICK,this,this.startClick)
        this.skinBtn.off(Laya.Event.CLICK,this,this.skinClick);
        this.wujinBtn.off(Laya.Event.CLICK,this,this.wujinClick);
        this.rankBtn.off(Laya.Event.CLICK,this,this.rankClick);
        if(Sdk.getIns().isLongScene())Sdk.getIns().hideBanner();
    }
}